//
//  MineCollectionViewCell.m
//  BLDMusics
//
//  Created by lxd on 16/3/17.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "MineCollectionViewCell.h"
#import "DSYButtonFTableViewCell.h"

@interface MineCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain) UITableView *mineTableView;   /**< 我的tableView */

@end

@implementation MineCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSbuviews];
        
        UINavigationController *nav =  [self getViewController];
        nav.navigationBar.translucent = YES;
        
        [nav.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor cyanColor] colorWithAlphaComponent:0.1]] forBarMetrics:UIBarMetricsDefault];
    }
    return self;
}

- (void)creatSbuviews {
    
    self.mineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - 124) style:UITableViewStylePlain];
    [self.contentView addSubview:_mineTableView];
    _mineTableView.delegate = self;
    _mineTableView.dataSource = self;
    _mineTableView.bounces = NO;
    
    [_mineTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    [_mineTableView registerClass:[DSYButtonFTableViewCell class] forCellReuseIdentifier:@"buttonFTableViewCell"];
    
    [self creatTableViewHeader];
}

- (void)creatTableViewHeader {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 2 / 3)];
    imgView.image = [UIImage imageNamed:@"dsy_backgroun.jpg"];
    _mineTableView.tableHeaderView = imgView;
}

#pragma mark - tableView的dataSource和delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        DSYButtonFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"buttonFTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return 2;
    }
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 150;
    }
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else {
        return 40;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return @"我的收藏";
    } else {
        return @"我创建的歌单";
    }
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    
    
}

- (UIImage *)imageWithColor:(UIColor *)color {
    
    // 描述矩形
    CGRect rect = CGRectMake(0, 0, 1, 1);
    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    
    return theImage;
}


// 获取当前navigation
- (UINavigationController*)getViewController {
    
    for (UIView* next = [self superview];next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController *)nextResponder;
            
        }
        
    }
    return nil;
}


@end
